import { _decorator, Component, Node, Vec3 } from 'cc';
import { SunMgr } from '../Mgr/SunMgr';
import { ModuleMgr } from '../ModuleMgr';
const { ccclass, property } = _decorator;

@ccclass('Sun')
export class Sun extends Component {

    private _sunNum: number = 25;
    // 掉落速度
    private _fallSpeed: number = 100;
    // 持续时间
    private _durationTime: number = 10;
    private _currentTime: number = 0;
    private _sunMgr: SunMgr = null;

    start() {
        this._sunMgr = ModuleMgr.instance.getModule("SunMgr") as SunMgr;
        this.node.on(Node.EventType.TOUCH_END, this.clickSun, this);
    }

    clickSun() {
        // 添加阳光
        this._sunMgr.addSun(this._sunNum);
        this._sunMgr.updateSunLabel();
        this.node.destroy();
    }

    update(deltaTime: number) {
        // 计算新的位置
        let currentPos = this.node.position;
        let newY = currentPos.y - this._fallSpeed * deltaTime;
        if (newY < -540) {
            this.node.destroy();
            return;
        }
        this.node.setPosition(new Vec3(currentPos.x, newY, currentPos.z));

        // 更新持续时间 销毁改为对象池
        this._currentTime += deltaTime;
        if (this._currentTime >= this._durationTime) {
            this.node.destroy();
        }
    }
}